/*
 * PassLogger.cpp
 *
 *  Created on: Oct 9, 2014
 *      Authors: Avihay Avitan & Stefan Farcas
 */

#include "PassLogger.h"

namespace LoggerNameSpace {

/**
 * create the pass logger object.
 * creates the new log file named in format: satId YYYY-MM-DD[HHMMSS].log
 * satId - the satellite id
 * passTime - the start time of the pass
 */
PassLogger::PassLogger (string satId , long passTime)
{
	timespec timespc;
	struct tm * timeinfo;
	char buffer [30];
	char filename [30];

	timespc.tv_sec = passTime ;
	timeinfo = localtime (&timespc.tv_sec);
    strftime (buffer,30,"%Y-%m-%d[%H%M%S]",timeinfo);
    sprintf(filename, "%s %s.log",satId.c_str(),buffer);
    this->fileName = filename;
	logger = new Logger(appendString(CoreSettings::getInstance().getPassLogDirPath(), filename));
	gsLogger = Logger::getGSLogger();
}

PassLogger::~PassLogger ()
{
	delete(logger);
}

/**
 * log the ProgressStatus info
 * status - the status to be logged
 * logToGSFile - is needed to be logged to the GSPASS.log file also?
 */
void PassLogger::logInfo (ProgressStatus status, bool logToGSFile)
{
	logInfo(status,LoggingColor::BLACK,logToGSFile);
}

/**
 * log the ProgressStatus info
 * status - the status to be logged
 */
void PassLogger::logInfo (ProgressStatus status)
{
	logInfo(status,LoggingColor::BLACK,false);
}

/**
 * log the ProgressStatus with ERROR type
 * status - the status to be logged
 * logToGSFile - is needed to be logged to the GSPASS.log file also?
 */
void PassLogger::logError (ProgressStatus status, bool logToGSFile)
{
	status.setIsToPublish(false);
	logger->logError(status);
	status.setIsToPublish(true);
	status.setIsFromConsole(false);
	if (logToGSFile)
		gsLogger->logError(status);
	else gsLogger->publish(status);
}
/**
 * log the ProgressStatus with WARNING type
 * status - the status to be logged
 * logToGSFile - is needed to be logged to the GSPASS.log file also?
 */
void PassLogger::logWarning (ProgressStatus status, bool logToGSFile)
{
	status.setIsToPublish(false);
	logger->logWarning(status);
	status.setIsToPublish(true);
	status.setIsFromConsole(false);
	if (logToGSFile)
		gsLogger->logWarning(status);
	else gsLogger->publish(status);

}

/**
 * log the ProgressStatus info with required color
 * status - the status to be logged
 * color - the required color
 * logToGSFile - is needed to be logged to the GSPASS.log file also?
 */
void PassLogger::logInfo (ProgressStatus status, LoggingColor color, bool logToGSFile)
{
	status.setIsToPublish(false);
	logger->logInfo(status,color);
	status.setIsToPublish(true);
	status.setIsFromConsole(false);
	if (logToGSFile)
		gsLogger->logInfo(status,color);
	else gsLogger->publish(status);

}
/**
 * log the ProgressStatus info with carriage return character in required color
 * status - the status to be logged
 * color - the required color
 * isFirst - whether the line is the first line or not (when false, it will override the prev line with the new one)
 * logToGSFile - is needed to be logged to the GSPASS.log file also?
 */
void PassLogger::logInfoCR(ProgressStatus status, LoggingColor color, bool isFirst, bool logToGSFile)
{
	status.setIsToPublish(false);
	logger->logInfoCR(status,color,isFirst);
	status.setIsToPublish(true);
	status.setIsFromConsole(false);
	if (logToGSFile)
		gsLogger->logInfo(status,color);
	else gsLogger->publish(status);
}

/**
 * returns the current satellite-pass log file name
 */
string PassLogger::getFileName()
{
	return this->fileName;
}

} /* namespace LoggerNameSpace */
